package cc.io24.knewcafe.boss.controller;

import cc.io24.knewcafe.boss.dao.SysUserDao;
import cc.io24.knewcafe.boss.domain.SysUser;
import cc.io24.knewcafe.boss.facade.PostFacade;
import cc.io24.knewcafe.boss.facade.model.Post;
import cc.io24.knewcafe.boss.service.PostService;
import cc.io24.knewcafe.boss.service.SysPermissionService;
import cc.io24.knewcafe.boss.service.SysUserService;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@Controller
public class IndexController {

    @Autowired
    private SysUserDao sysUserDao;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysPermissionService sysPermissionService;
    @Autowired
    private PostFacade postFacade;
    @Autowired
    private PostService postService;


    // @RequestMapping(value = "/")
    public String  hello(HttpServletRequest request, HttpServletResponse response){
        return "index.html";
    }

    // @RequestMapping(value="/index")
    public String  index(HttpServletRequest request, HttpServletResponse response){
        return "index";
    }

    @RequestMapping(value="/test",produces="application/json")
    @ResponseBody
    public Page<Post> test(@PageableDefault(value = 10, sort = { "title" }, direction = Sort.Direction.DESC)Pageable pageable){
        Post post = new Post();
        post.setContent("fuck");
        post.setTitle("fuck u");
        System.out.println();
        String sort = StringUtils.replace(pageable.getSort().toString(),":",",");
        sort = StringUtils.replace(sort," ","");
        return postService.getList(null,pageable);
    }


    //@RequestMapping(value = "/newUser",produces="application/json")
    //@ResponseBody
    public SysUser newUser(){
        SysUser user = new SysUser();
        user.setNickname("hello"+RandomUtils.nextInt(2000));
        user.setPassword("asdf");
        user.setUsername("user"+RandomUtils.nextInt(2000));
        user  = sysUserDao.save(user);
        return user;
    }

    //@RequestMapping(value = "/sysUser1",produces="application/json")
    //@ResponseBody
    public List<SysUser> usersList(@ModelAttribute SysUser sysUser,
                                   @PageableDefault(value = 10, sort = { "id" }, direction = Sort.Direction.DESC)Pageable pageable,
                                   HttpServletResponse response){
        Page<SysUser> page =  sysUserService.getList(sysUser,pageable);
        response.addHeader("X-Total-Count",page.getTotalElements()+"");
        response.addHeader("Access-Control-Expose-Headers","X-Total-Count");
        return page.getContent();
    }

}
